<?php

class DBPdoClass{
	
	private $dbType = 'mysql';
	private $dbHost = '127.0.0.1';
	private $dbName = 'jcc_news';
	private $dbUser = 'root';
	private $dbPwd = '';
	private $dsn = '';
	private $option = '';
	private $dbCharset = '';
	
	public static $link;
	public static $PDOStatement = null;
	public static $connected = false;
	public static $dbVersion = null;

	public $sql = '';
	public $rowNumber = 0;
	public $lastInsertID = null;
	
	/**
	 * 初始化属性
	 */
	public function __construct($pconnect = false){
		global $_ENV;
		
		if (!class_exists('PDO')) {
			throw new PDOException('服务器环境不支持PDO模块。');
			return false;
		}
		
		$this->dbType = dbType;
		$this->dbHost = dbHost;
		$this->dbName = dbName;
		$this->dbUser = dbUser; 
		$this->dbPwd = dbPwd;
		$this->dbCharset = dbCharset;
		$this->dsn = $this->dbType . ':host=' .  $this->dbHost . ';dbname=' . $this->dbName;
		$this->option = array(PDO::ATTR_PERSISTENT => $pconnect);
		
		if ($this->dbType == '') {
			throw new PDOException('没有设置数据库类型。');
			return false;
		}
		if ($this->dbHost == '') {
			throw new PDOException('没有设置数据库地址。');
			return false;
		}
		if ($this->dbName == '') {
			throw new PDOException('没有设置数据库名。');
			return false;
		}
	}
	
	/**
	 * 连接数据库
	 */
	public function connect() {
		self::$link = new PDO($this->dsn, $this->dbUser, $this->dbPwd, $this->option);
		if (!self::$link) {
            throw new PDOException('数据库连接失败。');
            return false;
        }
		self::$link->exec('SET NAMES ' . $this->dbCharset);
		self::$dbVersion = self::$link->getAttribute(constant("PDO::ATTR_SERVER_INFO"));
		self::$connected = true;
		return self::$link;
	}
	
	/**
	 * 关闭数据库连接 
	 */
	public function close(){
		if (self::$connected == true) {
			self::$link = null;
		}
	}
	
	/**
	 * 执行sql，进行数据库查询（select）
	 * 
	 * @param String $sql 执行的sql语句
	 */
	public function query($sql) {
		if ($sql == '') {
			throw new PDOException('SQL语句不能为空。');
			return false;
		}
		$this->sql = $sql;
		if (!self::$link) {
			throw new PDOException('没有连接可用的数据库。');
			return false;
		}
		$this->setFree();
		self::$PDOStatement = self::$link->prepare($this->sql);
		$resultSource = self::$PDOStatement->execute();
		$this->getPDOException();
		return $resultSource;
	}
	
	/**
	 * 执行sql，进行数据库操作（insert,update,delete）
	 * 
	 * @param String $sql 执行的sql语句
	 */
	public function execute($sql) {
		if ($sql == '') {
			throw new PDOException('SQL语句不能为空。');
			return false;
		}
		$this->sql = $sql;
		if (!self::$link) {
			throw new PDOException('没有连接可用的数据库。');
			return false;
		}
		$this->setFree();
		$count = self::$link->exec($this->sql);
		$this->getPDOException();
		$this->rowNumber = $count;
		$this->lastInsertID = self::$link->lastInsertId();
		return $this->rowNumber;
	}
	
	/**
	 * 查询sql，返回一条查询结果集
	 * 
	 * @param String $sql 执行的sql语句
	 */
	public function getOne($sql) {
		$this->query($sql);
		$result = self::$PDOStatement->fetch(constant('PDO::FETCH_OBJ'),constant('PDO::FETCH_ORI_NEXT'));
		return $result;
	}
	
	/**
	 * 查询sql，返回多条查询结果集
	 * 
	 * @param String $sql 执行的sql语句
	 */
	public function getAll($sql) {
		$this->query($sql);
		$result = self::$PDOStatement->fetchAll(constant('PDO::FETCH_OBJ'));
		return $result;
	}

	/**
	 * 获得异常
	 */
	private function getPDOException() {
		$obj = empty(self::$PDOStatement) ? self::$link : self::$PDOStatement;
		$error = $obj->errorInfo();
		if (count($error) > 1 && $error[2] != '') {
			throw new PDOException($error[2]. '<br/> [ SQL语句 ] : ' . $this->sql);
			return false;
		}
	}
	
	/**
	 * 释放查询结果集
	 */
	public function setFree() {
		if (!empty(self::$PDOStatement)) {
			self::$PDOStatement = null;
		}
	}
	
	/**
	 * 获得最后插入的一条自增长ID
	 * 
	 */
	public function getLastInsertID(){
		$this->lastInsertID = self::$link->lastInsetId();
		return $this->lastInsertID;
	}
}

?>